package com.music.note_android.ui.folder

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.music.note_android.R
import com.music.note_android.data.model.Note
import java.text.SimpleDateFormat
import java.util.*

class NoteSelectionAdapter(
    private val onNoteSelectionChanged: (Note, Boolean) -> Unit
) : ListAdapter<Note, NoteSelectionAdapter.NoteSelectionViewHolder>(NoteDiffCallback()) {

    private val selectedNoteIds = mutableSetOf<String>()
    private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteSelectionViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_note_selection, parent, false)
        return NoteSelectionViewHolder(view)
    }

    override fun onBindViewHolder(holder: NoteSelectionViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    fun getSelectedNoteIds(): List<String> {
        return selectedNoteIds.toList()
    }

    fun clearSelection() {
        selectedNoteIds.clear()
        notifyDataSetChanged()
    }

    inner class NoteSelectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val checkBox: CheckBox = itemView.findViewById(R.id.cb_note_selection)
        private val thumbnail: ImageView = itemView.findViewById(R.id.iv_note_thumbnail)
        private val title: TextView = itemView.findViewById(R.id.tv_note_title)
        private val time: TextView = itemView.findViewById(R.id.tv_note_time)

        fun bind(note: Note) {
            // 设置笔记标题
            title.text = if (note.title.isNotBlank()) {
                note.title
            } else {
                "无标题笔记"
            }
            
            // 设置笔记时间
            time.text = dateFormat.format(Date(note.modifiedTime))
            
            // 设置选择状态
            val isSelected = selectedNoteIds.contains(note.id)
            checkBox.isChecked = isSelected
            
            // 设置缩略图（这里可以根据笔记类型设置不同图标）
            thumbnail.setImageResource(R.drawable.ic_note_24)
            
            // 设置点击事件
            itemView.setOnClickListener {
                toggleSelection(note)
            }
            
            checkBox.setOnClickListener {
                toggleSelection(note)
            }
        }
        
        private fun toggleSelection(note: Note) {
            val isCurrentlySelected = selectedNoteIds.contains(note.id)
            
            if (isCurrentlySelected) {
                selectedNoteIds.remove(note.id)
            } else {
                selectedNoteIds.add(note.id)
            }
            
            // 更新UI
            checkBox.isChecked = !isCurrentlySelected
            
            // 回调选择变化事件
            onNoteSelectionChanged(note, !isCurrentlySelected)
        }
    }

    class NoteDiffCallback : DiffUtil.ItemCallback<Note>() {
        override fun areItemsTheSame(oldItem: Note, newItem: Note): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Note, newItem: Note): Boolean {
            return oldItem == newItem
        }
    }
}